<script setup lang="ts">
import { OrderType } from '@/enums'
import { uploaImg } from '@/services/consult'
import type { Image } from '@/types/consult'
import type { UploaderAfterRead } from 'vant/lib/uploader/types'
import { ref } from 'vue'

defineProps<{
  status?: OrderType
}>()

const emits = defineEmits<{
  (e: 'sendMsg', v: string): void
  (e: 'sendImg', v: Image): void
}>()
const text = ref('') // 输入框的值
const sendMsg = () => {
  // 子传父通知父组件 使用socket发送消息
  emits('sendMsg', text.value)
  // 清空输入框
  text.value = ''
}
const afterRead: UploaderAfterRead = async (obj) => {
  if (Array.isArray(obj)) return // 禁止多选
  if (!obj.file) return // 没选
  try {
    const res = await uploaImg(obj.file)
    // 通知父组件 发送的图片类型的消息
    emits('sendImg', res.data)
  } catch {
    console.log('失败了')
  }
}
</script>

<template>
  <div class="room-action">
    <van-field
      type="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      :disabled="status !== OrderType.ConsultChat"
      v-model.trim="text"
      @keyup.enter="sendMsg"
    ></van-field>
    <van-uploader
      :preview-image="false"
      :disabled="status !== OrderType.ConsultChat"
      :after-read="afterRead"
    >
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
